package com.aceitunaproject.server.message.processor;


import java.util.logging.Logger;

import com.aceitunaproject.server.player.Player;
import com.aceitunaproject.shared.exceptions.gamerules.IllegalActionException;
import com.aceitunaproject.shared.message.Message;
import com.aceitunaproject.shared.message.concrete.MoveRequest;
import com.aceitunaproject.shared.message.concrete.RuleViolationEvent;

/**
 * Handles the "Move" action request. Moves the unit to the destination map
 * tile, if the move is legal.
 * 
 * @author Federico Bergstein
 * 
 */
public class MoveRequestProcessor implements MessageProcessor {

	private static Logger logger = Logger.getLogger(MoveRequestProcessor.class
			.getName());

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void processMessage(Player player, Message message) {

		MoveRequest moveRequest = (MoveRequest) message;

		// TODO: Implement single logging
		logger.fine("ordered to move unit " + moveRequest.getElementId()
				+ " to " + moveRequest.getDestinationX() + " , "
				+ moveRequest.getDestinationY());
		Logger.getLogger(this.getClass().getName()).info("ordered to move unit " + moveRequest.getElementId()
				+ " to " + moveRequest.getDestinationX() + " , "
				+ moveRequest.getDestinationY());

		try {

			player.moveUnit(moveRequest.getElementId(), moveRequest.getDestinationX(),
					moveRequest.getDestinationY());

		} catch (IllegalActionException e) {
			
			RuleViolationEvent ruleViolation = new RuleViolationEvent();
			ruleViolation.setIllegalActionMessageID(message.getId());
			ruleViolation.setRuleViolations(e);
			player.sendObjectMessage(ruleViolation);
		}

	}

}
